<?php

namespace app\api\controller\v2;

use app\admin\model\Technician;
use think\facade\Db;
use app\common\controller\Frontend;

class Index extends Frontend
{
    protected array $noNeedLogin = ['*'];

    public function initialize (): void
    {
        parent::initialize();
    }


    /**
     * 获取当前城市是否开通
     * @return void
     */
    public function getCurrCityIsOpen (): void
    {
        $area_id = $this->request->param('area_id');
        $ret = (bool)Db::name('platform_area')->where('city_id', $area_id)->where('status', 1)->find();
        $this->success('当前城市开通状态', ['is_open' => $ret]);
    }


    /**
     * 获取最远距离
     * @return void
     */
    public function getDistance (): void
    {
        $this->success('', ['distance' => get_sys_config('farthest_order_distance')]);
    }


    public function getFareInfo ()
    {
        $param = $this->request->param();

        $lng = $param['lng'];
        $lat = $param['lat'];
        $t_id = $param['t_id'];
        $area_id = $param['area_id'];

        $info = Technician::where('technician_id', $t_id)->field('technician_id,lng,lat')->find();

        $km = sprintf('%.2f', distance($info->lat, $info->lng, $lat, $lng));

        $area = Db::name('platform_area')->where('city_id', $area_id)->find();


        $sunriseTimestamp = strtotime(date('Y-m-d', time()) . ' 04:53:00');
        $sunsetTimestamp = strtotime(date('Y-m-d', time()) . ' 19:27:00');

        $currentTimestamp = time();

        if ($currentTimestamp >= $sunriseTimestamp && $currentTimestamp <= $sunsetTimestamp) {
            $day = '白天';
            $starting_price = $area['day_starting_price'];
        } else {
            $day = '晚上';
            $starting_price = $area['night_starting_price'];
        }

        $str = "全程共{$km}公里，出租车出行收取来回费用，{$day}起步{$starting_price}元，超过3.00公里部分每公里{$area['per_kilometer_price']}元";

        if ($km > 3) {
            $price = bcadd($starting_price, bcmul(($km - 3), $area['per_kilometer_price'], 2), 2);
        } else {
            $price = $starting_price;
        }
        $price = bcmul($price, "2", 2);
        $this->success('', ['str' => $str, 'price' => $price]);
    }

    public function isTravel ()
    {
        $is_travel = Technician::where('technician_id', $this->request->param('t_id'))->value('is_travel');
        $this->success('', ['is_travel' => $is_travel]);
    }

}